package st.mark.quadtris;

import java.util.Random;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

public class BackgroundShapes {
	private Image blocks = null;
	
	// Number of blocks on screen
	private int block_count = 50; 
	// 0 = shape    1 = position    2 = speed	4 = rotate angle
	private Vector2f[][] block_states = new Vector2f[block_count][4]; 
	
	private Random rnd = new Random();
	
	public BackgroundShapes() throws SlickException{
		blocks = new Image("st/mark/resources/menu/menu_shapes.png");	
		
		// Init all of the block shapes
		for(int i = 0; i < block_count; i++){ 
			block_states[i][0] = new Vector2f(rnd.nextInt(3), rnd.nextInt(7)); // Shape orientation, Shape
			block_states[i][1] = new Vector2f(rnd.nextFloat()*800-48,rnd.nextFloat()*600-48); // Position
			
			float sx = rnd.nextFloat()/5, sy = rnd.nextFloat()/5;
			
			while(sx < 0.05f)
				sx = rnd.nextFloat()/5;
			
			while(sy < 0.05f)
				sy = rnd.nextFloat()/5;
			
			block_states[i][2] = new Vector2f(sx, sy); // Speed
			block_states[i][3] = new Vector2f(rnd.nextFloat()*2.5f, 0); // rotate
		}
	}
	
	public void render(){
		if(GameInfoData.SHOW_SHAPES){
			for(int i = 0; i < block_count; i++){
				Image tetrimino = blocks.getSubImage((int)(block_states[i][0].x * 48 + 1), (int)(block_states[i][0].y * 48 + 1), 47, 47);
				tetrimino.rotate(block_states[i][3].y);
				tetrimino.draw(block_states[i][1].x, block_states[i][1].y);
			}
		}
	}
	
	public void update(int delta){
		if(GameInfoData.SHOW_SHAPES){
			for(int i = 0; i < block_count; i++){
				block_states[i][1].x += (block_states[i][2].x * delta);
				block_states[i][1].y += (block_states[i][2].y * delta);
				
				block_states[i][3].y += block_states[i][3].x;
				if(block_states[i][1].x > 800 || block_states[i][1].y > 600){
					block_states[i][0] = new Vector2f(rnd.nextInt(3), rnd.nextInt(7)); // Shape orientation, Shape
					
					if(block_states[i][1].x > 800)
						block_states[i][1] = new Vector2f(-48,rnd.nextFloat()*600-48); // Position
					
					if(block_states[i][1].y > 600)
						block_states[i][1] = new Vector2f(rnd.nextFloat()*800-48,-48); // Position
					
					float sx = rnd.nextFloat()/5, sy = rnd.nextFloat()/5;
					
					while(sx < 0.05f)
						sx = rnd.nextFloat()/5;
					
					while(sy < 0.05f)
						sy = rnd.nextFloat()/5;
					
					block_states[i][2] = new Vector2f(sx, sy); // Speed
					block_states[i][3] = new Vector2f(rnd.nextFloat()*2.5f, 0); // rotate
				}
			}
		}
	}
}
